{extend name="index/nav" /}}
{block name="main"}
<div class="body">
    <div class="">
        <h2>资讯管理</h2>
    </div>
    <div class="index-table">
        <table class="am-table am-table-bordered am-table-radius am-table-striped" id="mytable">
            <thead>
            <tr>
                <th>gameID</th>
                <th>游戏名</th>
                <th>等级</th>
                <th>状态</th>
                <th>游戏简介</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>


<script src='/static/index/js/jquery-1.11.0.min.js'></script>
<script>
    $(function() {
        $('#collapse-nav4').addClass('am-in');
        //       屏幕高度
        var heigth = document.body.offsetHeight - 90;
        $('.body').css('height', heigth + 'px');

//      表格数据
        $.post('{:url("admin/index/togameAdmin")}', function (res) {
            res=JSON.parse(res);
            console.log(res);
            var result = [], hash = {};
            for (var i = 0; i<res.length; i++) {
                var elem = res[i].game_id;
                if (!hash[elem]) {
                    result.push(res[i]);
                    hash[elem] = true;
                }
            }
            res=result;
            for(var i= 0;i<res.length;i++){
                var data=res[i];
                data.grade=data.grade==1?'热门游戏':'游戏';
                data.down=data.down==1?'下载':'敬请期待';
                data.introduce=data.introduce==null?'暂无内容':data.introduce==''?'暂无内容':data.introduce;
                $('#mytable tbody').append(`<tr>
                   <td>${data.game_id}</td>
                <td class='game_name'><span>${data.name}</span></td>
                   <td class='news_grade'>${data.grade}</td>
                   <td class='game_down'><span>${data.down}</span></td>
                   <td class='game_introduce'><span>${data.introduce}</span></td>
                   <td> <a href="javascript:void(0)" class='image_add' data_id=${data.game_id}>编辑</a>
                        <a href="javascript:void(0)" class='imageAdmin' data_id=${data.game_id}>图片管理</a>
                        <a href="javascript:void(0)" class='image_delect' data_id=${data.game_id}>删除</a>
                   </td>
                </tr>`);
            };
        })
    })

// id 传值
    $('#mytable').delegate('.imageAdmin','click',function(){
        var id=$(this).attr('data_id');
        document.cookie=id;
        var arr=document.cookie.split(';');
        var id=arr[arr.length-1];
        console.log(id);
        window.location.href='{:url("admin/index/gameImage")}';
    })

    //  删除
    $('#mytable').delegate('.image_delect','click',function(){
        var id=$(this).attr('data_id');
        $.post('{:url("admin/index/delGame")}',{id:id},function (res) {
            window.location.reload();
        })
    })

    //  编辑
    $('#mytable').delegate('.image_add','click',function(){
        var id=$(this).attr('data_id');
        document.cookie= id;
        window.location.href='{:url("admin/index/gameEdit")}';
    })
</script>
{/block}